﻿// http://www.zimms.com
// Copyright (c) 2002-2007
// by James Zimmerman
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
// Software without restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
// Software, and to permit persons to whom the Software is furnished to do so, subject
// to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;

/// <summary>
/// Type mapping value object.
/// </summary>
public class TypeMapping : IEquatable<TypeMapping>
{
    #region Fields

    private readonly Type _contractType;
    private readonly Type _reificationType;

    #endregion

    #region Properties

    /// <summary>
    /// Gets the contract type.
    /// </summary>
    /// <value>The contract type.</value>
    public Type ContractType
    {
        get
        {
            return _contractType;
        }
    }

    /// <summary>
    /// Gets the type to contract reify.
    /// </summary>
    /// <value>The type to contract reify.</value>
    public Type ReificationType
    {
        get
        {
            return _reificationType;
        }
    }

    #endregion

    #region Overrides

    /// <summary>
    /// Determines whether the specified <seealso cref="Object"/> is equal to the current <seealso cref="Object"/>.
    /// </summary>
    /// <param name="obj">The <see cref="Object"/> to compare with the current <seealso cref="Object"/>.</param>
    /// <returns><see langkeyword="true">true</see> if the specified <seealso cref="Object"/> is equal to the current <seealso cref="Object"/>; otherwise, <see langkeyword="false">false</see>.</returns>
    public override Boolean Equals(Object obj)
    {
        if (obj is TypeMapping) return Equals(obj as TypeMapping);

        return base.Equals(obj);
    }

    #endregion

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the <see cref="TypeMapping"/> class.
    /// </summary>
    /// <param name="contractType">The type being mapped.</param>
    /// <param name="reificationType">The type to map with.</param>

    public TypeMapping(Type contractType, Type reificationType)
    {
        if (contractType == null) throw new ArgumentNullException("contractType");
        if (reificationType == null) throw new ArgumentNullException("reificationType");
        if (!contractType.IsAssignableFrom(reificationType))
            throw new ArgumentException(
                String.Format("Type '{0}' is not assignable from type '{1}'", contractType.FullName,
                              reificationType.FullName), "reificationType");

        this._contractType = contractType;
        this._reificationType = reificationType;
    }

    #endregion

    #region IEquatable<TypeMapping> Members

    /// <summary>
    /// Determines whether the specified <seealso cref="TypeMapping"/> is equal to the current <seealso cref="TypeMapping"/>.
    /// </summary>
    /// <param name="obj">The <see cref="TypeMapping"/> to compare with the current <seealso cref="TypeMapping"/>.</param>
    /// <returns><see langkeyword="true">true</see> if the specified <seealso cref="TypeMapping"/> is equal to the current <seealso cref="TypeMapping"/>; otherwise, <see langkeyword="false">false</see>.</returns>
    public Boolean Equals(TypeMapping obj)
    {
        return (
            obj != null &&
            obj._contractType.Equals(_contractType) &&
            obj._reificationType.Equals(_reificationType));
    }

    #endregion
}